/**
 * @author wucheshi
 * @time 2018-09-04
 * node add language file
 */
import translate from '../utils/translate'
import { isObject } from 'lodash-es'
let fs = require('fs')
let path = require('path')

// Chinese language file
const basicPath = path.resolve(__dirname, './lang')
// language list
const languageList = ['zh', 'en', 'jp']

/**
 * Rewrite entry file
*/
let indexStr =
`
${languageList.map(item => `import ${item} from './${item}'`).join('\n')}
const data = {
  ${languageList.join(',\n  ')}
}
export default data
`
fs.writeFileSync(`${basicPath}/index.js`, indexStr)

/**
 * Create other language dir and create file
 */
languageList.forEach(language => {
  if (language !== 'zh') {
    fs.mkdir(`${basicPath}/${language}`, (err) => {
      console.log(err)
    })
    generateLanguageFile(`${basicPath}/zh`, language)
  }
})

/**
 * Parse objects in file
 * @param {string} fileContent File content
 *
 * @returns {object} File internal object
 */
function parseFile (fileContent) {
  let clear = fileContent.replace(/(?:^|\n|\r)\s*\/\/.*(?:\r|\n|$)/g, '').replace(/\s+/g, '')
  let line = /\{(.)*\}/g.exec(clear)
  /* eslint-disable */
	return eval('(' + line[0] + ')')
  /* eslint-enable */
}

/**
 * Rewrite other language file content
 * @param {string} content Source file content
 * @param {object|array} transRulest Content after translation
*/
function reWriteFilteContent (content, transRulest) {
  let currentContent = content
  for (let i in transRulest) {
    if (isObject(transRulest[i])) {
      currentContent = reWriteFilteContent(currentContent, transRulest[i])
    } else {
      let regExp = new RegExp(i + ":(\\s*)(['])(.*?)(['])", 'g')
      let result = regExp.exec(currentContent)
      if (result) {
        currentContent = currentContent.replace(`${RegExp.$3}`, transRulest[i])
      }
    }
  }
  return currentContent
}

function mkdirs(dirpath) {
  if (!fs.existsSync(path.dirname(dirpath))) {
    mkdirs(path.dirname(dirpath))
  }
  fs.mkdirSync(dirpath)
}

/**
 * Create other language file
 * @param {string} filePath File path
 * @param {string} language language
 */
function generateLanguageFile(filePath, language) {
  fs.readdir(filePath, (err, files) => {
    if (err) {
      console.error(err)
    } else {
      files.forEach(fileName => {
        let filedir = path.join(filePath, fileName)
        let stat = fs.statSync(filedir)
        let isFile = stat.isFile()
        let isDir = stat.isDirectory()
        let newPath = filedir.replace(/zh/g, language)
        if (isFile) {
          let content = fs.readFileSync(filedir, 'utf-8')
          if (!fileName.includes('index')) {
            translate.toTranslate(parseFile(content), language).then(res => {
              let translateContent = reWriteFilteContent(content, res)
              fs.writeFileSync(newPath, translateContent, (err) => {
                if (err) {
                  throw err
                }
              })
            })
          } else {
            fs.writeFileSync(newPath, content, (err) => {
              if (err) {
                throw err
              }
            })
          }
        }
        if (isDir) {
          if (!fs.existsSync(newPath)) {
            mkdirs(newPath)
          }
          generateLanguageFile(filedir, language)
        }
      })
    }
  })
}
